# -*- coding: utf-8 -*- 
"""
Module Description:
Time : 2020/8/13 20:40 
Author : Wang P
File : activity_manager.py
version: 
"""
from common.service_config import get_server_id, get_remote_service_switch, get_app_id
from pyshare.const.enums.activity import ActivityType, ActivityClass, ActivityFlag, AllianceItemTypeDict, TypeToActor
from pyshare.const.enums.remote_service_switch import ServiceSwitch
from pyshare.dal.gameconfig.game_env import get_config_env_set
from pyshare.redis_cache.area.activity_redis_cache import CrossActivityList, CrossActivity, AreaRankActivityList, \
    RankActivity
from pyshare.utils.time_utils import addTimes, strToDatetime, getCT
from pyshare.dal.gameconfig.query import getProfile
from pyshare.bll.utils.area.rank_utils import new_rankManager


class RankActivityManager(object):
    def __init__(self):
        self.verify_time = get_config_env_set("RankVerifyTime")

    def verify_activity(self, activity, ac_type, verify_time, verify_cfg=None):
        """
        校验活动是否过了提示事件，不需要二次确认
        :param activity:
        :param ac_type:
        :param verify_time:
        :param verify_cfg:
        :return:
        """
        if not activity:
            return True
        if ac_type not in activity:
            return True
        if activity[int(ac_type)].get("EndTime") <= verify_time < addTimes(activity[int(ac_type)].get("EndTime"),
                                                                           seconds=self.verify_time):
            if ac_type in [ActivityType.SpecificMinisterAttribute, ActivityType.SpecificConcubineIntimate]:
                if verify_cfg and verify_cfg == activity[int(ac_type)].get("VerifyCfg"):
                    return False
                else:
                    return True
            return False
        return True

    @staticmethod
    def is_in_actor(activity_type, rid, activity_class=ActivityClass.CrossRank):
        """
        是否在资格名单中
        :param activity_type: 活动类型
        :param rid:
        :param activity_class:
        :return:
        """
        activity_type_cfg = getProfile('TcActivityType', Type=activity_type, Class=activity_class)
        if not activity_type_cfg:
            return False
        if not activity_type_cfg.NeedPreheat:
            return True
        actor_key = TypeToActor[activity_type].format(srv_id=get_server_id(), app_id=get_app_id())
        return False if new_rankManager.get_rank(rid, actor_key) == 0 else True


class AreaRankActivity(RankActivityManager):
    """
    区服类冲榜活动
    """
    def __init__(self):
        super(AreaRankActivity, self).__init__()
        self.area_activity = self.get_area_on_going_activity()

    @staticmethod
    def get_area_on_going_activity():
        current_activity = {}
        activity_id_list = AreaRankActivityList().get_all_rank_activity()
        if activity_id_list:
            for a_id in activity_id_list:
                activity = RankActivity().get_one_rank_activity(a_id)
                if not activity:
                    continue
                if strToDatetime(activity['EndTime']) <= getCT() < strToDatetime(activity['AwardEndTime']):
                    current_activity.update({int(activity["ActivityType"]): {"EndTime": strToDatetime(activity['EndTime']),
                                                                             "VerifyCfg": activity.get("SpecificId", 0)}})
        return current_activity


class CrossRankActivity(RankActivityManager):
    """
    跨服类冲榜活动
    """
    def __init__(self):
        super(CrossRankActivity, self).__init__()
        self.cross_activity = self.get_cross_on_going_activity()

    @staticmethod
    def get_cross_on_going_activity():
        current_activity = {}
        server_id = get_server_id()
        activity_id_list = CrossActivityList().get_all_activity()
        if activity_id_list:
            for a_id in activity_id_list:
                activity = CrossActivity().get_one_activity(a_id)
                if not activity or server_id not in activity['ServerIdList']:
                    continue
                if strToDatetime(activity['EndTime']) <= getCT() < strToDatetime(activity['AwardEndTime']):
                    current_activity.update(
                        {int(activity["ActivityType"]): {"EndTime": strToDatetime(activity['EndTime'])}})
        return current_activity


class RankActivityVerify(CrossRankActivity, AreaRankActivity):
    """
    冲榜活动提示校验
    """

    def verify_indicators(self, verify_time, flag=ActivityFlag.NotVerify, rid=None):
        """
        11015天赋强化、11016守护、15804妃子专宠、16703府邸扩建、16706府邸培养
        :param verify_time:
        :param flag:
        :param rid:
        :return:
        """
        if self.check_is_need_verify():
            if flag == ActivityFlag.DoVerify:
                # 跨服国力
                if not self.verify_activity(self.cross_activity, ActivityType.Comprehensive, verify_time):
                    if self.is_in_actor(ActivityType.Comprehensive, rid):
                        return False, ActivityClass.CrossRank, ActivityType.Comprehensive
                # 国力冲榜
                if not self.verify_activity(self.area_activity, ActivityType.Comprehensive, verify_time):
                    return False, ActivityClass.Rank, ActivityType.Comprehensive
        return True, 0, 0

    def verify_destiny(self, verify_time, verify_cfg=None, flag=ActivityFlag.NotVerify, rid=None):
        """
        11030悟性升级
        :param verify_time:
        :param verify_cfg:
        :param flag:
        :param rid:
        :return:
        """
        if self.check_is_need_verify():
            if flag == ActivityFlag.DoVerify:
                # 跨服国力
                if not self.verify_activity(self.cross_activity, ActivityType.Comprehensive, verify_time):
                    if self.is_in_actor(ActivityType.Comprehensive, rid):
                        return False, ActivityClass.CrossRank, ActivityType.Comprehensive
                # 跨服悟性
                if not self.verify_activity(self.cross_activity, ActivityType.CrossNumerology, verify_time):
                    if self.is_in_actor(ActivityType.CrossNumerology, rid):
                        return False, ActivityClass.CrossRank, ActivityType.CrossNumerology
                # 国力冲榜
                if not self.verify_activity(self.area_activity, ActivityType.Comprehensive, verify_time):
                    return False, ActivityClass.Rank, ActivityType.Comprehensive
                # 名臣冲榜 TODO 大臣id
                if not self.verify_activity(self.area_activity, ActivityType.SpecificMinisterAttribute, verify_time,
                                            verify_cfg):
                    return False, ActivityClass.Rank, ActivityType.SpecificMinisterAttribute
        return True, 0, 0

    def verify_indicators_minister(self, verify_time, verify_cfg=None, flag=ActivityFlag.NotVerify, rid=None):
        """
        11008资质果、11006典籍、11031名臣顿悟
        :param verify_time:
        :param verify_cfg:
        :param flag:
        :param rid:
        :return:
        """
        if self.check_is_need_verify():
            if flag == ActivityFlag.DoVerify:
                # 跨服国力
                if not self.verify_activity(self.cross_activity, ActivityType.Comprehensive, verify_time):
                    if self.is_in_actor(ActivityType.Comprehensive, rid):
                        return False, ActivityClass.CrossRank, ActivityType.Comprehensive
                # 国力冲榜
                if not self.verify_activity(self.area_activity, ActivityType.Comprehensive, verify_time):
                    return False, ActivityClass.Rank, ActivityType.Comprehensive
                # 名臣冲榜 TODO 大臣id
                if not self.verify_activity(self.area_activity, ActivityType.SpecificMinisterAttribute, verify_time,
                                            verify_cfg):
                    return False, ActivityClass.Rank, ActivityType.SpecificMinisterAttribute
        return True, 0, 0

    def verify_minister(self, verify_time, verify_cfg=None, flag=ActivityFlag.NotVerify, rid=None):
        """
        11003名臣升级
        :param verify_time:
        :param verify_cfg:
        :param flag:
        :param rid:
        :return:
        """
        if self.check_is_need_verify():
            if flag == ActivityFlag.DoVerify:
                # 跨服国力
                if not self.verify_activity(self.cross_activity, ActivityType.Comprehensive, verify_time):
                    if self.is_in_actor(ActivityType.Comprehensive, rid):
                        return False, ActivityClass.CrossRank, ActivityType.Comprehensive
                # 国力冲榜
                if not self.verify_activity(self.area_activity, ActivityType.Comprehensive, verify_time):
                    return False, ActivityClass.Rank, ActivityType.Comprehensive
                # 银两冲榜
                if not self.verify_activity(self.area_activity, ActivityType.CostSilvers, verify_time):
                    return False, ActivityClass.Rank, ActivityType.CostSilvers
                # 名臣冲榜 TODO 大臣id
                if not self.verify_activity(self.area_activity, ActivityType.SpecificMinisterAttribute, verify_time,
                                            verify_cfg):
                    return False, ActivityClass.Rank, ActivityType.SpecificMinisterAttribute
        return True, 0, 0

    def verify_intimate(self, verify_time, verify_cfg=None, flag=ActivityFlag.NotVerify, rid=None):
        """
        13102步摇/凤冠
        :param verify_time:
        :param verify_cfg:
        :param flag:
        :param rid:
        :return:
        """
        if self.check_is_need_verify():
            if flag == ActivityFlag.DoVerify:
                # 跨服亲密
                if not self.verify_activity(self.cross_activity, ActivityType.Intimate, verify_time):
                    if self.is_in_actor(ActivityType.Intimate, rid):
                        return False, ActivityClass.CrossRank, ActivityType.Intimate
                # 亲密冲榜
                if not self.verify_activity(self.area_activity, ActivityType.Intimate, verify_time):
                    return False, ActivityClass.Rank, ActivityType.Intimate
                # 妃子冲榜 TODO 妃子id
                if not self.verify_activity(self.area_activity, ActivityType.SpecificConcubineIntimate, verify_time,
                                            verify_cfg):
                    return False, ActivityClass.Rank, ActivityType.SpecificConcubineIntimate
        return True, 0, 0

    def verify_title_exp(self, verify_time, flag=ActivityFlag.NotVerify):
        """
        13102妃位包
        :param verify_time:
        :param flag:
        :return:
        """
        if self.check_is_need_verify():
            if flag == ActivityFlag.DoVerify:
                # 妃位冲榜
                if not self.verify_activity(self.area_activity, ActivityType.TitleExp, verify_time):
                    return False, ActivityClass.Rank, ActivityType.TitleExp
        return True, 0, 0

    def verify_silvers(self, verify_time, flag=ActivityFlag.NotVerify):
        """
        18723同盟国建设
        :param verify_time:
        :param flag:
        :return:
        """
        if self.check_is_need_verify():
            if flag == ActivityFlag.DoVerify:
                # 银两冲榜
                if not self.verify_activity(self.area_activity, ActivityType.CostSilvers, verify_time):
                    return False, ActivityClass.Rank, ActivityType.CostSilvers
        return True, 0, 0

    def verify_soldiers(self, verify_time, flag=ActivityFlag.NotVerify):
        """
        11302进攻副本、11307一键扫荡、18723同盟国建设
        :param verify_time:
        :param flag:
        :return:
        """
        if self.check_is_need_verify():
            if flag == ActivityFlag.DoVerify:
                # 耗兵冲榜
                if not self.verify_activity(self.area_activity, ActivityType.CostSoldiers, verify_time):
                    return False, ActivityClass.Rank, ActivityType.CostSoldiers
        return True, 0, 0

    def verify_alliance(self, verify_time, flag=ActivityFlag.NotVerify):
        """
        11102联盟礼花
        :param verify_time:
        :param flag:
        :return:
        """
        if flag == ActivityFlag.DoVerify:
            # 耗兵冲榜
            if not self.verify_activity(self.area_activity, ActivityType.AllianceExp, verify_time):
                return False, ActivityClass.Rank, ActivityType.AllianceExp
        return True, 0, 0

    def verify_item(self, verify_time, item_type, item_id, flag=ActivityFlag.NotVerify):
        """
        11102 使用道具
        :param verify_time:
        :param item_type:
        :param item_id:
        :param flag:
        :return:
        """
        if self.check_is_need_verify():
            if item_type in AllianceItemTypeDict:
                if AllianceItemTypeDict[item_type] == item_id:
                    return self.verify_alliance(verify_time, flag=flag)
        return True, 0, 0

    @staticmethod
    def check_is_need_verify():
        switch = get_remote_service_switch(ServiceSwitch.RankActivityVerify)
        return False if not switch else True


# rank_activity_verify = RankActivityVerify()


